package controllers;


	import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.UnregisteredUser;
import view.PendingRequestDetailsGUI;
import client.CliMessage;
import client.MailMessage;


		/**
		 * This Class is a Pending Registration Request Details Controller which  extends AbstractController
		 * @author Tal 
		 *
		 */
		public class PendingRegistrationRequestDetailsController extends AbstractController
		{	
					private PendingRequestDetailsGUI PendingRequestDetailsView;
					private UnregisteredUser unRegModel;
					
					/**
					 * This is Pending Registration Request Details controller constructor.
					 * @param PendingRequestDetailsView is a Pending Request Details Window instance.
					 * @param unRegModel is a Unregistered User Model instance.
					 */
					public PendingRegistrationRequestDetailsController(PendingRequestDetailsGUI PendingRequestDetailsView, UnregisteredUser unRegModel) 
					{
						super(PendingRequestDetailsView,unRegModel);
						this.PendingRequestDetailsView = PendingRequestDetailsView;
						this.unRegModel = unRegModel;
					}
					/**
					 * This is setButtons method-this method add listeners to buttons in Registration Request Details -approve/deny.
					 * @param  String id,UnregisteredUser unRegModel
					 */
					public void SetButtons(String id,UnregisteredUser unRegModel)
					{
						PendingRequestDetailsView.addApproveActionListener(new ApproveListener(id,unRegModel));
						PendingRequestDetailsView.addDenyActionListener(new DenyListener(id));
					}
					/**
					 * This is SendMail method-send mail to customer approve/deny registration
					 * @param String id, boolean-true/false
					 */
					
					public void SendMail(String user_ID,boolean flag)
					{
						MailMessage send = new MailMessage();
						
						if (flag)
						{
						send.sendMail(unRegModel.getEmail(),"Congratulations!!! "," Dear " + unRegModel.getLname() + " " + unRegModel.getFname() + ",\n Your Registration Was Approved By Our Customer Service to SGI, "
								+ "\nYour User ID is: " +user_ID+ ",  Your password is: " + unRegModel.getPassword()+"\n We Hope You Will Enjoy Our Services!!!");
						}
						else
							send.sendMail(unRegModel.getEmail(),"Sorry!!! "," Dear " + unRegModel.getLname() + " " + unRegModel.getFname() + ",\n Your Registration Was Denied By Our Customer Service to SGI");
					}

					/**
					 * Inner class that handles when Approve Button Pressed, implements Action listener
					 * @author Tal
					 *
					 */
					
					class ApproveListener implements ActionListener
					{
						private String uid;
						private UnregisteredUser regCus;
						
						public ApproveListener(String uid,UnregisteredUser regcus)
						{
							this.uid=uid;	
							this.regCus=regcus;
						}
								
						@Override
						public void actionPerformed(ActionEvent arg0) 
						{
							
							//change unregistered customer status in DB to Approved by Customer Service
							String updateQuary = "adu"+ "UPDATE unregisteredcustomer " +
								    					" SET isapprovedbycussrvc = 1 " +
								    					" WHERE urcid = " + uid;

							String pass = regCus.getPassword();
							String f = regCus.getFname();
							String l = regCus.getLname();
							String mail = regCus.getEmail();
							String p=regCus.getPhone();
							int log=0;
							
							//query
							String insertQuery = "adu"	+"INSERT INTO user "
								                    	+"(password,fname,lname,email,phone,loginstatus) VALUES "
								                    	+"('" + pass + "','" + f + "','" + l + "','" + mail + "','" + p + "'," + log + ")";
							
							
							//get the last uid that insert
							String searchQuery= "search" +"SELECT u.uid"
									                     + " FROM user AS u "
									                     + " ORDER BY u.uid DESC "
							                             + " LIMIT 1";
							sendToServer(updateQuary);
							sendToServer(insertQuery);
							sendToServer(searchQuery);
							
							
						
						}
					}
					

					/**
					 * Inner class that handles when Deny Button Pressed, implements Action listener
					 * @author Tal
					 *
					 */
					
					class DenyListener implements ActionListener
					{
								
                         private String uid;
						
						public DenyListener(String uid)
						{
							this.uid=uid;	
						}
								
						@Override
						public void actionPerformed(ActionEvent arg0) 
						{
							
							//change unregistered customer status in DB to Approved by Customer Service
							String updateQuary = "adu"+ "UPDATE unregisteredcustomer " +
								    					" SET isapprovedbycussrvc = 2 " +
								    					" WHERE urcid = " + uid;

							
							String searchQuery = "search"	+"SELECT unc.urcid,unc.fname,unc.lname "
															+" FROM unregisteredcustomer AS unc"
															+" WHERE unc.isapprovedbycussrvc=0";
							
							//activate the back button
							PendingRequestDetailsView.clickBackButton();
							
							
							sendToServer(updateQuary);
							sendToServer(searchQuery);
							
							//send denied mail to customer
					       SendMail("-1",false);
							
						}
					}


					@Override
					public void handleDBRequestSuccess()
					{
						PendingRequestDetailsView.displayWrnMessage("Request Details:");
						
					}

					@Override
					public void handleDBSearchResult(CliMessage dbResult) 
					{	
						//get Details tuple
						ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
						int rows=srs.size();
						String uid=null;
						String uid2=null;
						for(int i = 0 ; i <rows  ; i++)
						{
						   Map<String, String> RequestTupple  = srs.get(i);	
						   //the new uid that insert
						   uid2=RequestTupple.get("UID");
						   System.out.println("user id:"+RequestTupple.get("URCID")+" "
						   						+ ""+"fname:"+RequestTupple.get("FNAME")+" "
						   						+"lname:"+RequestTupple.get("LNAME")+" "
						   						+"address:"+RequestTupple.get("ADDR")+" "
						   						+"phone:"+RequestTupple.get("PHONE")+" "
						   						+"email:"+RequestTupple.get("EMAIL")+" "
						   						+"pass:"+RequestTupple.get("REQUESTEDPASSWORD"));
						 //initialize Unregistered User
						   if (uid2==null)
						   {
						   uid=RequestTupple.get("URCID");
						   unRegModel.setFname(RequestTupple.get("FNAME"));
						   unRegModel.setLname(RequestTupple.get("LNAME"));
						   unRegModel.setAddress(RequestTupple.get("ADDR"));
						   unRegModel.setPhone(RequestTupple.get("PHONE"));
						   unRegModel.setEmail(RequestTupple.get("EMAIL"));
						   unRegModel.setPassword(RequestTupple.get("REQUESTEDPASSWORD"));
						   }
						
						}	
						
						
						///set fields GUI
						
						PendingRequestDetailsView.setDetailsArea(unRegModel.toString());
						
						//set button listeners 
						 SetButtons(uid,unRegModel);
						 
						//display window
						PendingRequestDetailsView.setVisible(true);		
						
						
						//insert to user_group
						if (uid2!=null)
						{
							System.out.println(uid2);
							
							String insertQuery = "adu"	+"INSERT INTO user_groups "
			                    						+"(uid,gid) VALUES "
			                    						+"(" + uid2 + "," + 1 + ")";
							
							
							
							String searchQuery = "search"	+"SELECT unc.urcid,unc.fname,unc.lname "
															+" FROM unregisteredcustomer AS unc"
															+" WHERE unc.isapprovedbycussrvc=0";
							
							//activate the back button
							PendingRequestDetailsView.clickBackButton();
							
							sendToServer(insertQuery);
							sendToServer(searchQuery);
							
							//send Approve mail to customer
						       SendMail(uid2,true);
							

							
						}
						
					}

					@Override
					public void handleNullSearchResult() 
					{
						PendingRequestDetailsView.displayWrnMessage("Request Not Found!!!");
						
					}

}
